package com.jdj.framework.config.autoconfigure.properties;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

/**
 * Mybatis配置
 * @author caijinbang
 * @date 2019-04-14 11:00
 */
@ConfigurationProperties(prefix = MybatisProperties.PREFIX)
public class MybatisProperties {

    public static final String PREFIX = "framework.mybatis";

    /**
     * Mybatis配置文件路径
     */
    private String configLocation = "classpath:/META-INF/mybatis/mybatis-config.xml";

    /**
     * Mapper文件路径
     */
    private String[] mapperLocations;

    /**
     * 扫描基础的包的路径
     */
    private String basePackages;

    private Boolean customDaoBean = false;

    /**
     * 加载本地mapper资源
     * @return
     */
    public Resource[] resolveMapperLocations() {
        List<Resource> resources = new ArrayList<Resource>();
        if (this.mapperLocations != null) {
            for (String mapperLocation : this.mapperLocations) {
                Resource[] mappers;
                try {
                    mappers = new PathMatchingResourcePatternResolver().getResources(mapperLocation);
                    resources.addAll(Arrays.asList(mappers));
                } catch (IOException e) {
                }
            }
        }
        Resource[] mapperLocations = new Resource[resources.size()];
        mapperLocations = resources.toArray(mapperLocations);
        return mapperLocations;
    }

    public String getConfigLocation() {
        return this.configLocation;
    }

    public void setConfigLocation(String configLocation) {
        this.configLocation = configLocation;
    }

    public String getBasePackages() {
        return basePackages;
    }

    public void setBasePackages(String basePackages) {
        this.basePackages = basePackages;
    }

    public String[] getMapperLocations() {
        return this.mapperLocations;
    }

    public void setMapperLocations(String[] mapperLocations) {
        this.mapperLocations = mapperLocations;
    }

    public Boolean getCustomDaoBean() {
        return customDaoBean;
    }

    public void setCustomDaoBean(Boolean customDaoBean) {
        this.customDaoBean = customDaoBean;
    }

}
